<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f7fc;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        h1 {
            font-size: 24px;
            margin-bottom: 20px;
            color: #333;
        }

        label {
            font-size: 14px;
            color: #555;
            text-align: left;
            display: block;
            margin-bottom: 5px;
        }

        input {
            width: 100%;
            padding: 10px;
            margin: 10px 0 20px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
        }

        button:hover {
            background-color: #45a049;
        }

        p {
            font-size: 14px;
            color: #555;
        }

        a {
            color: #4CAF50;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }

        .captcha-container {
            margin: 20px 0;
        }

        .captcha-box {
            width: 100%;
            height: 50px;
            position: relative;
            border: 1px solid #ccc;
            margin-bottom: 10px;
        }

        .captcha-input {
            width: 100%;
            padding: 10px;
            margin-top: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>登录</h1>
        <form id="loginForm" method="POST">
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" required><br>
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" required><br>
            
            <!-- 添加数学验证码部分 -->
            <div class="captcha-container">
                <div id="captcha" class="captcha-box">
                    <canvas id="captchaCanvas" width="200" height="50"></canvas>
                </div><br>
                <label for="captchaInput">请输入数学题答案:</label>
                <input type="text" id="captchaInput" name="captcha" class="captcha-input" required>
            </div>
            
            <button type="submit">登录</button>
        </form>
        <p>还没有账户？<a href="{{ url_for('register') }}">注册</a>  <a href="{{ url_for('forget') }}">忘记密码？</a></p>
    </div>

    <script>
        // 生成随机的数学题并显示
        function generateCaptcha() {
            const num1 = Math.floor(Math.random() * 10) + 1;
            const num2 = Math.floor(Math.random() * 10) + 1;
            const correctAnswer = num1 + num2;

            // 将正确答案存储在表单中，但设置为不可见
            document.getElementById('loginForm').dataset.correctAnswer = correctAnswer;

            // 在验证码区域显示数学题
            const canvas = document.getElementById('captchaCanvas');
            const ctx = canvas.getContext('2d');
            ctx.clearRect(0, 0, canvas.width, canvas.height);

            // 设置字体
            ctx.font = "24px Arial";
            ctx.fillStyle = "#333";
            ctx.fillText(`${num1} + ${num2} = ?`, 50, 30);

            // 添加干扰线条
            for (let i = 0; i < 5; i++) {
                const x1 = Math.random() * canvas.width;
                const y1 = Math.random() * canvas.height;
                const x2 = Math.random() * canvas.width;
                const y2 = Math.random() * canvas.height;
                ctx.strokeStyle = `rgba(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255}, 0.7)`;
                ctx.lineWidth = 2;
                ctx.beginPath();
                ctx.moveTo(x1, y1);
                ctx.lineTo(x2, y2);
                ctx.stroke();
            }
        }

        // 验证用户输入的答案是否正确
        document.getElementById('loginForm').addEventListener('submit', function(event) {
            const userAnswer = parseInt(document.getElementById('captchaInput').value);
            const correctAnswer = parseInt(this.dataset.correctAnswer);

            if (userAnswer !== correctAnswer) {
                event.preventDefault(); // 阻止表单提交
                alert("验证码错误，请重新输入！");
            }
        });

        // 页面加载时生成验证码
        window.onload = generateCaptcha;
    </script>
</body>
</html>
